<template>
	<el-dialog title="公文接收" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="left">
			<el-form-item label="来文单位" prop="fromOrgName">
				<el-input v-model="form.fromOrgName" placeholder="来文单位" maxlength="50"></el-input>
			</el-form-item>
			<el-form-item label="收文标题" prop="title">
				<el-input v-model="form.title" placeholder="公文标题" maxlength="50"></el-input>
			</el-form-item>
			<el-form-item label="收文文号" prop="docNum">
				<el-input v-model="form.docNum" placeholder="收文文号" maxlength="50"></el-input>
			</el-form-item>
			<el-form-item label="收文类型" prop="recordId">
				<el-select v-model="form.recordId" style="width: 100%">
					<el-option v-for="item in flowList" :key="item.recordId" :value="item.recordId" :label="item.flowName"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="公文正文" prop="attachId" style="margin-top: 10px;">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'doc'}"
								:showFileList="false" @success="addNewFile"
								tip="请上传公文正文"
								accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-form-item label="备注" prop="remark" style="margin-top:10px">
				<el-input v-model="form.remark" maxlength="150" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">接 收</el-button>
		</template>
	</el-dialog>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
export default {
	components: {
		AttachMenu
	},
	emits: ['success', 'closed'],
	data() {
		return {
			apiObj:this.$API.file.attach.uploadFile,
			visible: false,
			isSaveing: false,
			fileList:[],
			flowList:[],
			//表单数据
			form: {
				revRecordId:'',
				recordId:'',
				title:"",
				docNum: "",
				fromOrgName:'',
				attachId:'',
				sendType: "0",
				remark: ""
			},
			//验证规则
			rules: {
				title: [
					{required: true, message: '公文标题不能为空'}
				],
				recordId: [
					{required: true, message: '收文类型不能为空'}
				],
				docNum: [
					{required: true, message: '公文文号不能为空'}
				],
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(){
			this.visible = true;
			return this
		},
		//表单提交方法
		submit(){

			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.bpm.bpmFormData.createBpmByTransferRule.post(this.form);
					this.isSaveing = false;
					if(res.code == 200){
						this.$nextTick(() => {
							this.$router.replace({
								path: '/doc/handle/dowork',
								query:{
									runId:res.data.runId,
									stepRunId:res.data.stepRunId,
									flowId:res.data.flowId
								}
							})
							this.visible = false;
							this.$message.success("操作成功")
						})
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})

		},
		async bpmTransferRule(){
			let res = await this.$API.bpm.bpmTransferRule.getBpmTransferRuleList.get({sendType:this.form.sendType});
			if(res.code == 200){
				this.flowList = res.data;
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		setData(data){
			this.form.revRecordId = data.recordId
			this.form.sendType = data.sendType
			this.form.title = data.title
			this.form.docNum = data.docNum
			this.form.fromOrgName = data.fromOrgName
			this.form.attachId = data.attachId
			this.form.model = data.model
			this.form.remark = data.remark
			this.bpmTransferRule();
		},
	}
}
</script>

<style>
</style>
